package cn.tedu.service;

import cn.tedu.pojo.Car;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@Component
public class CarServiceImel implements CarService{

    //public void get(Car car) throws Exception {
    public Car get() throws Exception {
        //将数据插入数据库
        //Class.forName("com.mysql.jdbc.Driver");
        //String url = "jdbc:mysql:///cgb2104?characterEncoding=utf8&useSSL=false";
        //Connection conn = DriverManager.getConnection(url,"root","admin123");
        //
        //String sql = "insert into car values(?,?,?)";
        //PreparedStatement ps = conn.prepareStatement(sql);
        //ps.setString(1,car.getName());
        //ps.setString(2,car.getColor());
        //ps.setDouble(3,car.getPrice());
        //
        //ps.executeUpdate();
        //ps.close();
        //conn.close();
        //System.out.println("数据入库成功！");

        //查询数据库
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql:///cgb2104?characterEncoding=utf8&useSSL=false";
        Connection conn = DriverManager.getConnection(url,"root","root");

        String sql = "select * from car where name=?";
        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setString(1,"bsj");
        ResultSet rs = ps.executeQuery();

        Car c = new Car();
        while (rs.next()){
            c.setName(rs.getString(1));
            c.setColor(rs.getString(2));
            c.setPrice(rs.getDouble(3));
        }
        rs.close();
        ps.close();
        conn.close();
        return c;
    }
}
